show dbs
show databases
use  lg_resume
db.createCollection("lg_resume_preview")
show  dbs
show  tables
show  collections
db.dropDatabase()

db.lg_resume_preview.insert({name:"张晓峰",birthday:new ISODate("2000-07-01"),
expectSalary:15000,city:'bj'})
 //Copy from NoSQLBooster for MongoDB free edition. This message does not appear if you are using a registered version.

ObjectId("5eb3ce52479dfe245ca9af77").getTimestamp()
  
db.lg_resume_preview.insert([
    {name:"张晓丽",birthday:new ISODate("2001-04-01"),
expectSalary:25000,city:'bj'},
    {name:"李丽",birthday:new ISODate("2002-06-01"),
expectSalary:18000,city:'sh'}])  

db.lg_resume_preview.find()
db.lg_resume_preview.find({city:"bj"})
db.lg_resume_preview.find({expectSalary:18000})

/** 更新expectSalary:18000 更新成 28000 */
db.lg_resume_preview.update({expectSalary:18000}, 
    {$set:{expectSalary:28000}}, 
    { multi: false, upsert: false}
)
db.lg_resume_preview.find({expectSalary:28000})

db.lg_resume_preview.update({expectSalary:38000}, 
    {$set:{expectSalary:28000}}, 
    { multi: false, upsert: true}
)

db.lg_resume_preview.find({})

db.lg_resume_preview.update({name:"李丽"}, 
    {$inc:{expectSalary:-10000}}, 
    { multi: false, upsert: false}
)
db.lg_resume_preview.update({name:"李丽"}, 
    {$unset:{expectSalary:""}}, 
    { multi: false, upsert: false}
)
db.lg_resume_preview.update({name:"李丽"}, 
    {expectSalary:18000}, 
    { multi: false, upsert: false}
)
db.lg_resume_preview.find({name:"李丽"})
db.lg_resume_preview.find({expectSalary:18000})

db.lg_resume_preview.remove({expectSalary:18000})
db.lg_resume_preview.find()
db.lg_resume_preview.remove({city:"bj"}, {justOne: true})

/** 删除数据 */
db.lg_resume_preview.remove({})
/** 插入数据 */
db.lg_resume_preview.insert({name:"张晓峰",birthday:new ISODate("2000-07-01"),gender:1,expectSalary:15000,city:"bj"})
db.lg_resume_preview.insert({name:"张震",birthday:new ISODate("1995-04-01"),gender:1,expectSalary:18000,city:"bj"})
db.lg_resume_preview.insert({name:"李山",birthday:new Date("1995-04-01"),gender:1,expectSalary:25000,city:"sh"})
db.lg_resume_preview.insert({name:"李笑来",birthday:new Date("1998-04-01 14:20:09"),gender:1,expectSalary:20000,city:"sh"})
db.lg_resume_preview.insert({name:null,birthday:new Date("1992-04-01 14:20:09"),gender:1,expectSalary:30000,city:"sh"}) 
db.lg_resume_preview.insert({birthday:new Date("1991-05-01 14:20:09"),gender:1,expectSalary:50000,city:"sz"})   

db.lg_resume_preview.insert(
    [{name:"李丽",birthday:new Date("1996-05-01 14:20:09"),gender:0,expectSalary:21000,city:"sz"},
     {name:"李平",birthday:new Date("1997-07-01 14:20:09"),gender:0,expectSalary:22000,city:"sz"}])
db.lg_resume_preview.find().count()
db.lg_resume_preview.distinct("city")
db.lg_resume_preview.count()

/** 按照city 进行分组 统计每个city出现的次数*/
db.lg_resume_preview.aggregate([{$group: { _id: "$city",city_count:{$sum:1}}}])
db.lg_resume_preview.aggregate({$group: { _id: "$city",city_count:{$sum:1}}})

/** 按照city 进行分组 统计每个city 中expectSalary的平均值  */
db.lg_resume_preview.aggregate([{$group: { _id: "$city",avg_sal:{$avg:"$expectSalary"}}}])

/** 按照city 进行分组 统计每个city的值放入一个数组中  */
db.lg_resume_preview.aggregate([{$group: { _id: "$city",city_name:{$push:"$city"}}}])
db.lg_resume_preview.aggregate([{$group: { _id: "$city",city_name:{$addToSet:"$city"}}}])

db.lg_resume_preview.aggregate(
[{$group : {_id: "$city", avgSal:{$avg:"$expectSalary"}}},
 {$project : {city: "$city",  sal : "$avgSal"}}
])

db.lg_resume_preview.aggregate(
[{$group:{_id: "$city",count:{$sum : 1}}},
 {$match:{count:{$gte:2}}}
]) 

db.lg_resume_preview.aggregate(
[{$group:{_id: "$city",city_count:{$sum : 1}}},
 {$match:{city_count:{$gte:2}}}
])

db.lg_resume_preview.createIndex({name:1})
db.lg_resume_preview.getIndexes()

db.lg_index_test.insert({name:"test1",salary:18000})
db.lg_index_test.insert({name:"test2",salary:29000,birth_day:new ISODate("2001-05-02")})
db.lg_index_test.find()
db.lg_index_test.getIndexes()
db.lg_index_test.createIndex({birth_day:1},{expireAfterSeconds:31})

db.company.insert(
   {
     loc : { type: "Point", coordinates: [ 116.482451, 39.914176 ] },
     name: "大望路地铁",
     category : "Parks"
   }
)

db.company.insert(
   {
     loc : { type: "Point", coordinates: [ 116.492451, 39.934176 ] },
     name: "test1",
     category : "Parks"
   }
)

db.company.insert(
   {
     loc : { type: "Point", coordinates: [ 116.462451, 39.954176 ] },
     name: "test2",
     category : "Parks"
   }
)


db.company.insert(
   {
     loc : { type: "Point", coordinates: [ 116.562451, 38.954176 ] },
     name: "test3",
     category : "Parks"
   }
)

db.company.insert(
   {
     loc : { type: "Point", coordinates: [ 117.562451, 37.954176 ] },
     name: "test4",
     category : "Parks"
   }
)

db.company.find()
db.company.ensureIndex( { loc : "2dsphere" } )
db.company.find({
    "loc" : { 
        "$geoWithin" : {
          "$center":[[116.482451,39.914176],0.05] 
        }
    }
})

/** 计算中心点最近的三个点 */
db.company.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [116.482451,39.914176 ] },
        key: "loc",
        distanceField: "dist.calculated"
     }
   },
   { $limit: 3 }
])
/** 全文索引  倒排索引 */
db.textTextIndex.insert({id:1,name:"test1",description:"one world one dream in bj",city:"bj"})
db.textTextIndex.insert({id:2,name:"test2",description:"two world two dream in nj",city:"nj"})
db.textTextIndex.insert({id:3,name:"test3",description:"dj is not bj and nj",city:"dj"})
db.textTextIndex.insert({id:4,name:"test4",description:"dj is not bj and nj",city:"dj"})

db.textTextIndex.find()
db.textTextIndex.createIndex({description:"text"});
db.textTextIndex.find({"$text": {"$search": "two"}})
db.textTextIndex.createIndex({name:1},{background:true})
db.textTextIndex.getIndexes()
db.textTextIndex.totalIndexSize()

db.textTextIndex.reIndex()

db.textTextIndex.dropIndex("name_1")
db.textTextIndex.dropIndex("_id_")
db.textTextIndex.dropIndexes()

db.textTextIndex.find({name:"test1"})
db.textTextIndex.dropIndexes()

/** 向lg_resume 中插入100 万条数据 */
for(var i=0;i<1000000;i++){
    db.lg_resume.insert({id:i,name:"test"+i,salary:(Math.random()*20000).toFixed(2)});
}
db.lg_resume.count()
db.lg_resume.find({name:"test11011"}).explain()
db.lg_resume.find({name:"test11011"}).explain("executionStats")

db.lg_resume.find({id:{$gt:222333}}).explain()
db.lg_resume.createIndex({id:1})
db.lg_resume.find({id:{$gt:222333}}).explain("executionStats")
db.lg_resume.find({name:{$gt:"test222333"}}).explain("executionStats")
/*限定返回字段 */
db.lg_resume.find({name:{$gt:"test222333"}},{_id:0}).explain("executionStats")

db.resume.find()
db.lg_resume_datas.find()
db.lg_resume_datas.createIndex({name:1},{unique:true})
db.lg_resume_datas.dropIndex({name:1})
db.lg_resume_datas.insert({name:"zhangsan",city:"sh",expectSalary:25000})
db.lg_resume_datas.insert({name:"zhangsan",city:"sh",expectSalary:25000})

db.lg_resume_datas.find({name:"lisi22"})
// 5ec26852878f6732d523034a




